package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.IDCardBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpMethods;
import com.eqdd.yiqidian.http.HttpMultiUtil;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Group;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by lvzhihao on 17-3-28.
 */

public class PhotoHttpUtil {
    public static void getPhotoInfo(BaseActivity baseView, String filePath,
                                    HttpPresneter.OnHttpListener onHttpListener) {
        if (baseView != null) {
            baseView.showLoading();
        }
        Subscriber<String> subscriber = new Subscriber<String>() {

            @Override
            public void onStart() {
                super.onStart();

            }

            @Override
            public void onCompleted() {
                if (baseView != null) {

                    baseView.hideLoading();
                }
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("错误" + e.toString());
                if (baseView != null) {

                    baseView.hideLoading();
                }

                onHttpListener.onError(e);

            }

            @Override
            public void onNext(String t) {
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    if (!jsonObject.has("error_message")) {
                        IDCardBean idCard = new Gson().fromJson(t, IDCardBean.class);
                        if (idCard.getCards().size() > 0 && idCard.getCards().get(0).getSide().equals("front")) {
                            onHttpListener.onNext(idCard.getCards().get(0));
                        } else {
                            ToastUtil.showLong("请上传正面身份证照");
                        }
                    } else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        String url = "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard";
        Map<String, RequestBody> partMap = new HashMap<>();
        partMap.put("api_key", HttpMultiUtil.createPartFromString(HttpConfig.FACE_PLUSPLUS_APIKEY));
        partMap.put("api_secret", HttpMultiUtil.createPartFromString(HttpConfig.FACE_PLUSPLUS_APTSECRET));
        HashMap<String, String> map = new HashMap<>();
        map.put("image_file",filePath);
        HttpMethods.getInstance().getDataFromServerMultipart(baseView, subscriber, String.class, url,
                new HashMap<String, String>(), partMap, map);


    }
}
